/*********************************************************************
 *
 *                         action操作;
 *
 *********************************************************************/
'use strict';

import RequestApis from '@/api/index';

import Utils from '@/utils/index';

/**
 * 根据地址和高度查询交易流水
 */
interface AccountTransactionsType{
   address: string
   height: number
   index: number
}
export const getAccountTransactionsRequestAction = ({ commit }, _params:AccountTransactionsType):Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {
            console.log("index="+_params.index);

            let res: any = await RequestApis.getAccountTransactionsRequest(_params.address, _params.height, _params.index);
                console.log("账户res");
                console.log(res);
            if(res.data==false||res.data=="false"){
                resolve([]);

            }else{
                resolve(res.response.data);

            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}

/**
 * 根据地址查询代币交易流水
 */
export const getAccountTransactionsAnotherRequestAction = ({ commit }, _params: AccountTransactionsType): Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {

            let res: any = await RequestApis.getAccountTransactionsAnotherRequest(_params.address, _params.index);
            console.log("代币res");
            console.log(res);
            if (res.data == false || res.data == "false") {
                resolve([]);

            } else {
                resolve(res.response.data);

            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}


// 根据地址查询交易的数量
export const getAccountTransactionsCountAction = ({ commit }, _address:string):Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {
            console.log("账户tranCount");
            // let res: any = await RequestApis.getAccountTransactionsCount(_address);
            let res: any = await RequestApis.getAccountTransactionsCount(_address);
                console.log("账户tranCount");
                console.log(res);
            if(res.data==false||res.data=="false"){
                resolve([]);

            }else{
                resolve(res.response.data);

            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}


/**
 * 根据地址和高度查询交易流水(根据类型和hash查询)
 */
interface AccountTransactionsForHashType{
    type: number
    hash:  string
 }
 export const getAccountTransactionsForHashRequestAction = ({ commit }, _params:AccountTransactionsForHashType):Promise<any> => {

     return new Promise(async (resolve, reject) => {

         try {

             let res: any = await RequestApis.getAccountTransactionsForHashRequest(_params.type, _params.hash);

             if(res.status == 200){

                if(res.response.data.success == true){

                   resolve(res.response.data);
                }

             }else{

                 resolve([]);
             }

         } catch (error) {

             reject(error);
             return;
         }
     })

 }


/**
 * 查交易详情的接口
 */
 export const getAccountTransactionsDetailRequestAction = ({ commit }, _hash:string):Promise<any> => {

     return new Promise(async (resolve, reject) => {

         try {

             let res: any = await RequestApis.getAccountTransactionsDetailRequest(_hash);

                // if(res.response.data.success == true){

                if(res.response.data == false||res.response.data=="false"){
                    reject('err');
                    //debugger
                 }else{
                    resolve(res.response.data);
                //     //debugger
                }



         } catch (error) {

             reject(error);
             return;
         }
     })

 }

export const getTXBlockRequestAction = ({ commit }, _hash:string):Promise<any> => {
    return new Promise(async (resolve, reject) => {
        try {
            let res: any = await RequestApis.getTXBlockRequest(_hash);
            console.log("交易res");
            console.log(res);
             if (res.response.data == false||res.response.data=="false") {
                reject('err');
             }else{
                resolve(res.response.data);
             }
        } catch (error) {
            reject(error);
            return;
        }
    })
}


 /**
 * 查交易详情的接口
 */
export const getLatestMcBlockRequestAction = ({ commit }):Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {

            let res: any = await RequestApis.getLatestMcBlockRequest();

            if(res.status == 200){

               if(res.response.data.success == true){

                  resolve(res.response.data.mcBlockList);
               }

            }else{

                resolve([]);
            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}


/**
 * 查交易详情的接口
 */
export const globalInfoRequestAction = ({ commit }):Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {

            let res: any = await RequestApis.globalInfoRequest();

            if(res.status == 200){

               if(res.response.data.success == true){

                  resolve(res.response.data);
               }

            }else{

                resolve([]);
            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}

/*** 最近出块 */
export const getLatestBlockRequestAction = ({ commit }, _hash:string): Promise<any> => {
    return new Promise(async (resolve, reject) => {

        try {

            let res: any = await RequestApis.getLatestBlockRequest(_hash);
            // console.log("res.status="+res.status);
            if(res.status === 200){
            //    if(res.response.data.success == true){
                console.log('res=');
                console.log(res);
                resolve(res.response.data);
            //    }
            } else {
                resolve([]);
            }

        } catch (error) {
            reject(error);
            return;
        }
    })
}

function JSONLength(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

/*** 最近出块 */
export const getNumberOfSlicesRequestAction = ({ commit }): Promise<any> => {
    return new Promise(async (resolve, reject) => {
        try {
            let res: any = await RequestApis.getNumberOfSlicesRequest();
            // console.log("res.status="+res.status);
            if(res.status === 200){
                //    if(res.response.data.success == true){
                console.log('res=');
                console.log(res);
                resolve(JSONLength(res.response.data));
                //    }
            } else {
                resolve([]);
            }

        } catch (error) {
            reject(error);
            return;
        }
    })
}


/**
 * 支点分布
 */
export const getGlobalNodeDistInfoRequestAction = ({ commit }):Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {

            let res: any = await RequestApis.getGlobalNodeDistInfoRequest();

            if(res.status == 200){

               if(res.response.data.success == true){

                  resolve(res.response.data.nodeDistInfoList);
               }

            }else{

                resolve([]);
            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}


/**
 * 获取全网高度
 */
export const getLatestBlockHeightRequestAction = ({ commit }):Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {

            let res: any = await RequestApis.getLatestBlockHeightRequest();
            console.log("res=");
            console.log(res);
            if(res.status == 200){

               if(res.response.data.success == true){

                  resolve(parseInt(res.response.data.height));
               }

            }else{

                resolve(0);
            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}


/**
 * 获取余额
 */
export const getbalanceRequestAction = ({ commit }, _address:string):Promise<any> => {

    return new Promise(async (resolve, reject) => {

        try {

            let res: any = await RequestApis.getbalanceRequest(_address);

            if(res.status == 200 || res.success != false){

                resolve(res.response.data.result);

            }else{

                resolve(0);
            }

        } catch (error) {

            reject(error);
            return;
        }
    })

}


